<?php
namespace backend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\data\Pagination;


/**
* User model
*/
class ModelListModel extends ActiveRecord
{
	const STATUS_DISACTIVE = 0;
	const STATUS_ACTIVE = 1;
	const DEFAULT_PAGE_NUM = 1;
	const DEFAULT_PAGE_ROWS = 20;

	/**
	* @inheritdoc
	*/
	public static function tableName()
	{
		return '{{%backend_model_list}}';
	}

	/**
	* @inheritdoc
	* 自动更新creat_at和update_at字段
	*/
	public function behaviors()
	{
		return [
			TimestampBehavior::className(),
		];
	}

	/**
	* @inheritdoc
	*/
/*
	public function rules()
	{
		return [
		['status', 'default', 'value' => self::STATUS_ACTIVE],
		['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DISACTIVE]],
		];
	}
	*/
	/**
	* 取得所有记录
	*/
	public static function RowList()
	{
		return self::find()->all();
	}

	/**
	* 按ID检索记录
	*/
	public static function findIdentity($id)
	{
		$result =self::findOne(['id' => $id]);
		if (empty($result)) {
			return false;
		}
		return $result->attributes;
	}
	/**
	 * 取得检索结果
	 * @param   $where['sql_fields'] 检索的字段逗号拼接，例如  id,title,times
	 * @param   $where['sql_order'] 检索的结果排序，例如  id desc,times asc
	 * @param   $where['per-page'] 检索的结果每页条数限制  INT类型
	 * @param   $where['page'] 检索的结果页数  INT类型
	 * @param   $where[other...] 其它字段检索字段
	 * @return  Array
	 */
	public  static function _search($where=array())
	{
		$result=array();

		//设置清除检索记录中的非Where 字段
		if ( isset($where['sql_fields'] )) { 	$sql_fields=trim($where['sql_fields']); 	unset($where['sql_fields']);  }else{ $sql_fields="*"; }
		if ( isset($where['sql_order'] )) { 	$sql_order=trim($where['sql_order']); 	unset($where['sql_order']);  }else{ $sql_order="id desc"; }
		if ( isset($where['per-page']) ) {
			if (intval($where['per-page']) >0) {
				$sql_limit=intval($where['per-page']);
				unset($where['per-page']);
			} else {
				$sql_limit=self::DEFAULT_PAGE_ROWS;
				unset($where['per-page']);
			}

		}else{
			$sql_limit=self::DEFAULT_PAGE_ROWS;
		}
		if ( isset($where['page']) ) {
			if (intval($where['page']) >0) {
				$sql_page=intval($where['page']);
				unset($where['page']);
			} else {
				$sql_page=self::DEFAULT_PAGE_NUM;
				unset($where['page']);
			}
		}else{
			$sql_page=self::DEFAULT_PAGE_NUM;
		}


		//检索符合的总记录和条件记录内容
		$result['total']=self::find()->where($where)->count();
		$result['rows']=self::find()
			->select($sql_fields)
			->where($where)
			->orderBy($sql_order)
			->limit($sql_limit)
			->offset(($sql_page-1)*$sql_limit)
			->asArray()
			->all();


		$result['pages'] = new Pagination(['totalCount' =>$result['total'], 'pageSize' => $sql_limit]);
		return $result;
	}

	/**
	 * 添加修改
	 * @param  $data['id'] 存在ID时为更新对应id字段，否则为新增
	 * @param   $data[other...] 其它字段为待新增/更新字段
	 */
	public static function _edit($data)
	{
		if (empty($data['id'])) {
			$rows=new self();
		} else {
			$rows = self::findOne($data['id']);
		}
		unset($data['id']);

		if (empty($data)) {
			return false;
		} else {
			foreach ($data as $k => $v) {
				$rows->$k=$v;
			}
		}
		//print_r($rows);
		// if (isset($data['uid'])) { $rows->uid=$data['uid'] ;}
		// if (isset($data['lid'])) { $rows->lid=$data['lid'] ;}
		// if (isset($data['count'])) { $rows->count=$data['count'] ;}
		if ($rows->save()) {
			return $rows->id;
		}else{
			 var_dump($rows->getErrors());exit;

			return false;
		}


	}


	/**
	 * 删除
	 * @param true/false
	 */
	public  static function _del($id)
	{
		$TpRecord = Tpview::findOne($id);
		if($TpRecord){
			$TpRecord->delete();
			return true;
		}else{
			return false;
		}
	}

}
